/* Module Test Functions */
 
#include "test.h"
#include "utils.h"
#include "keyboard.h"
#include "uart.h"
#include "lcd1602.h"
#include "wifi.h"
#include "hx71x.h"
#include "vcom.h"
#include "led.h"
#include "filter.h"
#include "eeprom.h"
#include "beep.h"
#include "debug.h"

void test_keyboard(void)
{
    uchar keycode;
    uint i;
    char keycode_str[3];

    vcom_init(19200);

    while(1)
    {
        while(!iskeyinput()); // wait for key press
        for(i =0; i<500; i++); //

        if(iskeyinput()) // exactly key pressed
        {
            keycode = key_identify();
            wait_key_release();
            num2char(keycode, keycode_str);
            vcom_send(keycode_str);
        }
    }
}

void test_lcd1602(void)
{
    uchar code text1[15]="LCD1602 TEST";   //第一行要显示的字符
    uchar code text2[15]="Hello Wolrd";   //第二行要显示的字符

    lcd1602_init();

    lcd1602_show(0, 0, text1);
    lcd1602_show(1, 0, text2);

    while(1);

    /*
       lcd1602_write_com(0x80);   //0x80是第一行的第一个字符的地址
       for(i=0;i<11;i++)
       lcd1602_write_data(text1[i);

       lcd1602_write_com((uchar )0x80+0x40);   //0xc0是第二行的第一个字符的地址
       for(j=0;j<11;j++)
       lcd1602_write_data(text2[j);
       */
}

void test_wifi(void)
{
    delay(10000);
    beep_init();
    wifi_init();
    lcd1602_init();
    lcd1602_show(0,0,"WiFi Uart Test");

    while(1)
    {
        // wifi_send("esp8266 is live");

        if(wifi_rx_ok)
        {
            wifi_send("esp8266 receive:");
            wifi_send(wifi_rx_buff);
            lcd1602_part_clear(1,0,16);
            lcd1602_show(0,0,"WiFi receive:");
            lcd1602_show(1,0,wifi_rx_buff);
            wifi_rx_ok = 0;
            wifi_rx_count = 0;
        }
    }

}

void test_uart(void)
{
    uchar idata str[10];
    uart_init(115200);
    uart_enable_recv();
    // uart_enable_it();

    while(1)
    {
        // delay(60000);
        
        // uart_send("hello world\r\n");
        // delay(10000);

        
        uart_recv(str, 8);
        uart_send(str);
        uart_send("\r\n");
        
    }
}

void test_hx71x(void)
{
    ulong data;
    char idata str[9];

    // uart_init(115200);
    vcom_init(19200);
    beep_init();
    led_init();
    lcd1602_init();
    lcd1602_show(0,0, "hx711 ADC:");

    while(1)
    {
        // hx71x_read(&data);
        // moving_avaraging_filter(&data);
        data = digital_averaging_filter(hx71x_read);
        num2char(data, str);
        debug(str);
        debug("\r\n");
        lcd1602_part_clear(1,0,10);
        lcd1602_show(1,0, str);
        led_toggle(1);
        delay(20000);
    }
}

#define VCOM_BAUDRATE 19200
void test_vcom(void)
{
    uchar str[10];

    led_init();
    vcom_init(VCOM_BAUDRATE);
    vcom_enable_recv(VCOM_BAUDRATE);

    while(1)
    {

        vcom_recv(str, 5);
        vcom_send(str);
        vcom_send("\r\n");

        /*
           vcom_send("Virtual Com Test...\r\n");
           vcom_send("Hello World");
           vcom_send("\r\n");
           delay(60000);
           */
    }
}

void test_led(void)
{
    led_init();
    while(1)
    {
        led_toggle(1);
        led_toggle(2);
        delay(60000);
    }
}

void test_eeprom(void)
{
    WORD i;

    // uchar dat[2];
    vcom_init(19200);

    vcom_send("Init successful\r\n");
    IapEraseSector(IAP_ADDRESS); //Erase current sector
    for (i=0; i<512; i++) //Check whether all sector data is FF
    {
        if (IapReadByte(IAP_ADDRESS+i) != 0xff)
        {
            vcom_send("IAP operation fail\r\n");
            while (1);
        }
    }

    vcom_send("Erase successful\r\n");

    delay(10); //delay
    for (i=0; i<512; i++) //Program 512 bytes data into data flash
    {
        IapProgramByte(IAP_ADDRESS+i, (BYTE)i);
    }
    vcom_send("Program successful\r\n");

    delay(100); //delay
    for (i=0; i<512; i++) //Verify 512 bytes data
    {
        /*
        *dat = IapReadByte(IAP_ADDRESS+i);
        *(dat+1) = '\0';
        vcom_send(dat);
        */

        if (IapReadByte(IAP_ADDRESS+i) != (BYTE)i)
        {
            vcom_send("IAP operation fail\r\n");
            while (1);
        }


    }
    vcom_send("Verify successful\r\n");

    while (1);
}

void test_beep(void)
{
    beep_init();

    while(1)
    {
        beep_start();
        delay(50000);
        beep_stop();
        delay(50000);
    }
}